package com.meeting.meeting.ErrorInterceptor;




import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;



/**
 * 自定义拦截器
 *
 * @author hxl
 * @create 2018/12/14
 */


@Controller
public class ErrorInterceptor implements HandlerInterceptor {

    private static final Log logger = LogFactory.getLog(ErrorInterceptor.class);



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        logger.info("【-MyInterceptor1,在请求处理之前进行调用(controller方法调用之前)-】");
        //创建session


        HttpSession session =request.getSession();
        Object loginName=session.getAttribute("loginUser");
        if (loginName==null||"".equals(loginName)){
            response.sendRedirect("/");
        }



        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        logger.info("【-MyInterceptor1,请求处理之后进行调用，但是在视图被渲染之前(controller方法调用之后)-】");

       if(response.getStatus()==500){
            response.sendRedirect("/500");


        }else if(response.getStatus()==404){
            response.sendRedirect("/404");
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        logger.info("【-MyInterceptor1,在整个请求结束之后被调用,也就是在DispatcherServlet渲染了对应的视图之后执行(主要用于资源清理工作)-】");
    }

}

